/**
 * User Controller
 * 
 * @author Tomas Holub
 * @package OverFish
 */

var of = require('OverFish'),
    sys = require('sys'),
    User = require('../models/User').User,
    userRepo = require('../repositories/UserRepository');

exports.actions = new Array();

/**
 * default Action
 */
exports.actions.push({
  'action': 'default',
  'callback': function (req) {
  
    context = {'name': 'New Default'};
    
    userRepo.all(function(err, users) {
        if (err) throw err;
        context['user'] = new User();
        context['users'] = users;
        
        of.render('index', context);
    });
  
    
  }
});

/**
 * add Action
 */
exports.actions.push({
  'action': 'add',
  'callback': function (req) {
      context = {'name': 'New Default'};
      userRepo.all(function(err, users) {
        if (err) throw err;
        context['user'] = new User();
        context['users'] = users;
        users.forEach(function(usr) {
          context['user'] = usr;
          sys.puts(sys.inspect(usr.username()));
        });
        
        of.render('add', context);
    });
  }
});


/*var gh = require('grasshopper'),
    userRepo = require('../repositories/UserRepository'),
    User = require('../models/User').User,
    sys = require('sys');

gh.get('/admin/user', function() {
    var self = this;
    userRepo.all(function(err, users) {
        self.model['user'] = new User();
        self.model['users'] = users;
        self.render('test/index');
    });
});

gh.post('/admin/user', function() {
    var self = this;
    var user = new User().update(this.params);
    if(user.isValid()) {
        userRepo.save(user, function() {
            self.flash['success'] = 'User was succesfully saved';
            self.redirect('/');
        });
    } else {
        userRepo.all(function(err, users) {
            self.model['users'] = users;
            self.model['user'] = user;
            self.render('test/index');
        });
    }
});
*/